package com.demo.tests.cases;

import com.demo.config.ApiEndpoints;
import com.demo.utils.LogUtils;
import io.qameta.allure.*;
import io.restassured.RestAssured;
import io.restassured.response.Response;
import org.apache.logging.log4j.Logger;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;

import static org.testng.Assert.assertEquals;

@Feature("API测试套件")
@Story("购物车接口测试")
public class DemoTestNoData {
    private static final Logger log = LogUtils.getLogger();

    @BeforeClass
    public void setUp() {
        log.info("===== 开始初始化测试配置 =====");
//        Response response = RestAssured.given()
//                .header("Auth-Token", "Bearer eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpc3MiOiIiLCJhdWQiOltdLCJqdGkiOiJXRUFEMm92Uk50IiwiaWF0IjoxNzU0MDM3NjgxLCJuYmYiOjE3NTQwMzc2ODAsImV4cCI6MTc1NTMzMzY4MSwiaWQiOiJNVFk1TVh3eE56VTBNRE0zTmpneGZETTJNRGN3TURFek56bG1PRGszTURNNFlXTXpNalF4WW1ZeU1tTXhOR1JtIn0.YqRfwmViZ6Vgz2u9Ub-Bp105qhC0nK6lA1saOljApgk")
//                .header("Origin", "http://10.59.9.14:7003")
//                .header("Content-Type", "application/json");
//        log.info("===== 初始化测试配置结束 =====");
    }

    @Test(groups = "a3")
    @Severity(SeverityLevel.CRITICAL)
    public void testPostRichtext() {
        log.info("开始执行POST {}接口测试", ApiEndpoints.RONCOO_ADDSER);

        // 修正请求体为JSON格式，匹配截图中的请求体结构
        String requestBody = "{\"id\":1000111,\"sku_id\":\"\",\"num\":1}";
        log.debug("POST请求体: {}", requestBody);

        Response response = RestAssured.given()
                .header("Origin", "http://10.59.9.14:7003")
                .header("auth-token", "Bearer eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpc3MiOiIiLCJhdWQiOltdLCJqdGkiOiJXRUFEMm92Uk50IiwiaWF0IjoxNzU0MDM3NjgxLCJuYmYiOjE3NTQwMzc2ODAsImV4cCI6MTc1NTMzMzY4MSwiaWQiOiJNVFk1TVh3eE56VTBNRE0zTmpneGZETTJNRGN3TURFek56bG1PRGszTURNNFlXTXpNalF4WW1ZeU1tTXhOR1JtIn0.YqRfwmViZ6Vgz2u9Ub-Bp105qhC0nK6lA1saOljApgk")
                .body(requestBody)
                .post(ApiEndpoints.getFullUrl(ApiEndpoints.RONCOO_ADDSER));
        log.info(ApiEndpoints.RONCOO_ADDSER);

        log.info("POST请求响应状态码: {}", response.statusCode());
        log.debug("POST请求响应体: {}", response.asPrettyString());

        // 验证响应状态码是否为200
        assertEquals(response.statusCode(), 200, "接口请求失败，状态码非200");

        // 添加Allure报告附件
        Allure.addAttachment("请求体", requestBody);
        Allure.addAttachment("响应状态码", String.valueOf(response.statusCode()));
        Allure.addAttachment("响应体", response.asPrettyString());
        log.info(response.asPrettyString());
    }
}